﻿using AnlianCommon.Common.Domains.OrganizationalUnits;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.Industrys
{
    public class SecondIndustryQueryHandler : IRequestHandler<SecondIndustryQueryCommand, Result>
    {
        private readonly IBaseRepository<SecondLevel> _secondLevelRepository;

        public SecondIndustryQueryHandler(IBaseRepository<SecondLevel> secondLevelRepository)
        {
            _secondLevelRepository = secondLevelRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SecondIndustryQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code=(int)CommonErrorCode.Search};
            try
            {
                var secondLevel = await (await _secondLevelRepository.GetAll()).Where(c=>c.FirstLevelId==request.FirstLevelId).ToListAsync();
                result.Data = secondLevel;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
